2020-2021 Sunseeker Telemetry and Lighting System
ctsd16_ex1_singleChContConv.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430FG662x Demo - CTSD16, Continuous Rail-to-Rail Conversion, Single Channel
34 //
35 // Description: This program uses the CTSD16 module to perform continuous
36 // conversions on a single channel with rail-to-rail input. A CTSD16 interrupt
37 // occurs when a conversion has completed.
38 //
39 // Test by applying voltages to the input channel and setting a breakpoint
40 // at the indicated line. Run program until it reaches the breakpoint, then use
41 // the debugger's watch window to view the conversion results.
42 //
43 // Results (upper 16 bits only) are stored in the array "results"
44 //
45 // ACLK = 32kHz, MCLK = SMCLK = Calibrated DCO = 16.384MHz, SD_CLK = 1.024MHz
46 //
47 // Notes: For minimum Vcc required for CTSD16 module - see datasheet
48 // 1nF cap btw Vref and AVss is recommended when using 1.2V ref
49 //
50 // MSP430FG662x
51 // -----------------
52 // /|\| |
53 // | | |
54 // --|RST |
55 // | |
56 // Vin1+ -->|A0.0+ VREF |---+
57 // Vin1- -->|A0.0- | |
58 // | | -+- 1nF
59 // | | -+-
60 // | | |
61 // | AVss |---+
62 //
72 //******************************************************************************
73 
74 #include "driverlib.h"
75 
76 uint32_t results[8];
77 
78 void main(void) {
79  // Stop WDT
80  WDT_A_hold(WDT_A_BASE);
81 
82  // Select AD0+/- analog input pins
83  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P6, GPIO_PIN4 | GPIO_PIN5);
84 
85  // Initialize CTSD16 using internal reference and internal resistor for clock
86  CTSD16_init(CTSD16_BASE,
87  CTSD16_RTR_INPUT_CHARGEPUMP_BURST_REQUEST_DISABLE, CTSD16_REF_INTERNAL);
88 
89  // Initialize converter 0: AD0+ / AD0- as input, 2s complement, channel 9
90  CTSD16_initConverterAdvancedParam convAdvParam = {0};
91  convAdvParam.converter = CTSD16_CONVERTER_0;
92  convAdvParam.conversionMode = CTSD16_CONTINUOUS_MODE;
93  convAdvParam.groupEnable = CTSD16_NOT_GROUPED;
94  convAdvParam.inputChannel = CTSD16_INPUT_CH9;
95  convAdvParam.dataFormat = CTSD16_DATA_FORMAT_2COMPLEMENT;
96  convAdvParam.railToRailInput = CTSD16_RTR_INPUT_ENABLE;
97  convAdvParam.interruptDelay = CTSD16_FOURTH_SAMPLE_INTERRUPT;
98  convAdvParam.oversampleRatio = CTSD16_OVERSAMPLE_256;
99  convAdvParam.gain = CTSD16_GAIN_1;
100  CTSD16_initConverterAdvanced(CTSD16_BASE, &convAdvParam);
101 
102  // Clear converter 0 interrupt flags
103  CTSD16_clearInterrupt(CTSD16_BASE, CTSD16_CONVERTER_0,
104  CTSD16_CONVERTER_INTERRUPT|CTSD16_CONVERTER_OVERFLOW_INTERRUPT);
105  // Enable converter 0 overflow, result interrupts
106  CTSD16_enableInterrupt(CTSD16_BASE, CTSD16_CONVERTER_0,
107  CTSD16_CONVERTER_INTERRUPT|CTSD16_CONVERTER_OVERFLOW_INTERRUPT);
108 
109  // Delay ~120us for 1.2V ref to settle
110  __delay_cycles(2000);
111 
112  // Wait for rail-to-rail input ready
113  while(!CTSD16_isRailToRailInputReady(CTSD16_BASE));
114 
115  // Set bit to start conversion
116  CTSD16_startConverterConversion(CTSD16_BASE, CTSD16_CONVERTER_0);
117 
118  __bis_SR_register(LPM0_bits | GIE); // Enter LPM0 w/ interrupts
119 }
120 
121 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
122 #pragma vector=CTSD16_VECTOR
123 __interrupt
124 #elif defined(__GNUC__)
125 __attribute__((interrupt(CTSD16_VECTOR)))
126 #endif
127 void CTSD16_ISR(void) {
128  static uint8_t index = 0;
129  switch (__even_in_range(CTSD16IV,CTSD16IV_CTSD16MEM0)) {
130  case CTSD16IV_NONE: break;
131  case CTSD16IV_CTSD16OVIFG:
132  // Clear overflow interrupt explicitly
133  CTSD16_clearInterrupt(CTSD16_BASE, CTSD16_CONVERTER_0, CTSD16_CONVERTER_OVERFLOW_INTERRUPT);
134  break;
135  case CTSD16IV_CTSD16MEM0:
136  // Save CH0 results (clears IFG)
137  results[index++] = CTSD16_getResults(CTSD16_BASE, CTSD16_CONVERTER_0);
138  if(index >= 8) {
139  index = 0; // SET BREAKPOINT HERE
140  }
141  break;
142  default: break;
143  }
144 }
uint32_t results[8]
void main(void)
void CTSD16_ISR(void)
__delay_cycles(500000)